package _02array;

public class _03InsertAtEndUnSortArray {


    public static void main(String[] args) {
        int[] arr = new int[10];
        int[] arrBase = {10, 8, 30, 40, 20};
        System.arraycopy(arrBase, 0, arr, 0, arrBase.length );

        int capacity = arr.length;
        int endIndex = 4;

        endIndex = insertAtEnd(arr, endIndex + 1, 15, capacity);
        System.out.println("After insert:");
        for (int i = 0; i <= endIndex; i++)
            System.out.println(arr[i]);
    }

    private static int insertAtEnd(int[] arr, int index, int key, int capacity) {
        // Cannot insert, index more than or equal to capacity
        if (index >= capacity)
            return index - 1;

        // insert
        arr[index] = key;

        return index;
    }
}
